// 订单Mixins

import {
	apiCommunityGroupPurchaseOrderClose,
} from '@/api/community_group_purchase'

const OrderMixin = {
	data() {
		return {
			// 订单按钮组
			ButtonsMap: {
				'payment': {
					event: 'payment',
					name: '立即付款',
					style: 'primary'
				},
				'content': {
					event: 'content',
					name: '查看内容',
					style: 'muted'
				},
				'close': {
					event: 'close',
					name: '取消订单',
					style: 'muted'
				},
				'pickup': {
					event: 'pickup',
					name: '提货凭证',
					style: 'primary'
				},
				'call': {
					event: 'call',
					name: '联系团长',
					style: 'muted'
				}
			},
			LeaderButtonsMap: {
				'content': {
					event: 'content',
					name: '查看内容',
					style: 'muted'
				},
				'close': {
					event: 'close',
					name: '取消订单',
					style: 'muted'
				},
				'pickup': {
					event: 'pickup',
					name: '核销',
					style: 'primary'
				},
			}
		}
	},

	methods: {
		// 获取订单状态按钮组
		getOrderStatusButtons(buttonStatus) {
			let buttons = []

			// 取消
			buttonStatus.cancel_btn && buttons.push(this.ButtonsMap['close'])

			// 查看内容
			buttonStatus.content_btn && buttons.push(this.ButtonsMap['content'])
			// 提货码
			buttonStatus.pickup_btn && buttons.push(this.ButtonsMap['pickup'])
			// 联系团长
			buttonStatus.call_btn && buttons.push(this.ButtonsMap['call'])
			// 支付
			buttonStatus.pay_btn && buttons.push(this.ButtonsMap['payment'])

			return buttons
		},
		// 获取订单状态按钮组
		getLeaderOrderStatusButtons(buttonStatus) {
			let buttons = []

			// 查看内容
			buttonStatus.content_btn && buttons.push(this.LeaderButtonsMap['content'])
			// 提货码
			buttonStatus.pickup_btn && buttons.push(this.LeaderButtonsMap['pickup'])

			return buttons
		},

		// 点击订单按钮映射处理方法
		onOrderButtons(event, orderData) {
			switch (event) {
				case 'payment':
					return this.handlePayment(orderData.id)
				case 'close':
					return this.handleClose(orderData.id, orderData.cgp_lo_id)
				case 'call':
					return this.handleCall(orderData.leader_phone)
				case 'content':
					return this.handleContent(orderData.id)
				case 'pickup':
					return this.handlePickup(orderData)
			}
		},

		// 点击订单按钮映射处理方法
		onLeaderOrderButtons(event, orderData) {
			switch (event) {
				case 'content':
					return this.handleContent(orderData.id)
				case 'pickup':
					return this.handleVerificationPickup(orderData.pickup_code)
			}
		},

		// 刷新订单数据
		refreshOrderData() {
			throw new Error('未初始化刷新方法')
		},

		// 处理：支付
		handlePayment(orderID) {
			this.$Router.push({
				path: `/bundle2/pages/payment/payment`,
				query: {
					from: 'cgp_order',
					order_id: orderID
				}
			})
		},

		handleContent(orderID) {
			this.$Router.push({
				path: `/bundle2/pages/community_group_purchase/order/order_detail`,
				query: {
					order_id: orderID
				}
			})
		},

		// 处理：关闭订单
		handleClose(orderID, cgp_lo_id) {
			uni.showModal({
				title: '温馨提示',
				content: '是否取消订单?',
				confirmColor: this.themeColor,
				success: ({
					confirm
				}) => {
					if (!confirm) return
					// uni.showLoading()
					// 取消订单
					apiCommunityGroupPurchaseOrderClose({
						id: orderID,
						cgp_lo_id
					}).then(data => {
						this.$toast({
							title: '取消成功'
						})
						this.refreshOrderData()
					}).catch(err => {
						console.log(err)
					}).finally(() => {
						// uni.hideLoading()
					})
				}
			})
		},

		// 处理：联系团长
		handleCall(leader_phone) {
			uni.makePhoneCall({
				phoneNumber: leader_phone.toString()
			})
		},
	}
}

export default OrderMixin